package com.ruoyi.ruoyiqywechat.service.py;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Service
public class PythonService {

    @Value("${py_mp4_to_mp3_url}")
    private String pyMp4ToMp3Url;
    @Value("${py_mp4_to_mp3_output_dir}")
    private String pyMp4ToMp3OutputDir;

    public  String convertVideoToMp3(String videoPath) {
        try {
            // 构建Python命令
            ProcessBuilder processBuilder = new ProcessBuilder(
                "python", pyMp4ToMp3Url, videoPath, pyMp4ToMp3OutputDir
            );

            Process process = processBuilder.start();

            // 读取Python脚本的输出
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream())
            );

            StringBuilder output = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }

            int exitCode = process.waitFor();
            if (exitCode == 0) {
                return output.toString();
            } else {
                return "转换失败";
            }

        } catch (Exception e) {
            throw new RuntimeException("Python脚本执行失败", e);
        }
    }

//    public static void main(String[] args) {
//        String string = convertVideoToMp3("D:\\idea_work_temp\\20241219zhicheng\\spy\\mp4tomp3\\33.mp4", "2output_audio.mp3");
//        System.out.println(string);
//    }
}
